package com.hontech.bvapp2.ui

import android.content.Context
import android.hardware.display.DisplayManager
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.hontech.bvapp2.R
import com.hontech.bvapp2.app.globalInit
import com.hontech.bvapp2.ui.fragment.Cargo1Fragment
import com.hontech.bvapp2.ui.fragment.Cargo2Fragment
import com.hontech.bvapp2.ui.fragment.DoorFragment
import com.hontech.bvapp2.ui.fragment.DropFragment
import com.hontech.bvapp2.ui.fragment.HeatFragment
import com.hontech.bvapp2.ui.fragment.SettingFragment
import com.hontech.bvapp2.ui.lcd.Lcd
import com.hontech.bvapp2.utils.EmptyEdge
import com.hontech.bvapp2.utils.runOnUi


class MainActivity : BaseActivity() {

    private lateinit var mTabInfo: TabInit

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mTabInfo = TabInit(this)
        openLcd()
    }

    private fun openLcd() {
        val displayManager = getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
        val ds = displayManager.displays
        if (ds.size > 1) {
            val d2 = ds[1]
            val lcd = Lcd(this, d2)
            lcd.show()
        }
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            hideButton()
            globalInit()
        }
    }

}


private class TabInfo(
    val text: String,
    val iconId: Int,
    val fragment: Fragment
)

private class TabInit(activity: MainActivity) : FragmentStateAdapter(activity) {

    private val mVp2: ViewPager2
    private val mTable: TabLayout

    private val tabInfos = arrayOf(
        TabInfo("设置", R.drawable.ic_setting, SettingFragment()),
        TabInfo("切膜落杯", R.drawable.ic_setting, DropFragment()),
        TabInfo("货架1", R.drawable.ic_setting, Cargo1Fragment()),
        TabInfo("货架2", R.drawable.ic_setting, Cargo2Fragment()),
        TabInfo("加热", R.drawable.ic_setting, HeatFragment()),
        TabInfo("门", R.drawable.ic_setting, DoorFragment()),
    )

    init {
        val vp2 = activity.findViewById<ViewPager2>(R.id.id_main_vp2)
        val tabs = activity.findViewById<TabLayout>(R.id.id_main_tablayout)
        mVp2 = vp2
        mTable = tabs

        vp2.adapter = this
        vp2.offscreenPageLimit = tabInfos.size
        TabLayoutMediator(tabs, vp2, true, false) { tab, index ->
            tab.text = tabInfos[index].text
            tab.setIcon(tabInfos[index].iconId)
        }.attach()
        vp2.isUserInputEnabled = false
        (vp2.getChildAt(0) as RecyclerView).edgeEffectFactory = EmptyEdge()
    }

    override fun getItemCount(): Int {
        return tabInfos.size
    }

    override fun createFragment(position: Int): Fragment {
        return tabInfos[position].fragment
    }
}
